/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * 
 * 		Author: tThirday 
 * 		Please report bugs to http://eyecm.com.
 * 		Any problems, please leave messages on http://eyecm.com.
 */
package com.eyecm.connector;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;


import org.apache.log4j.Logger;

import com.eyecm.exception.ConnectorException;
import com.eyecm.model.Contact;
import com.eyecm.model.User;
import com.google.gdata.client.Query;
import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.util.AuthenticationException;


public class GmailConnector extends BaseConnector implements ConnectorInterface
{

	private static final String CONNECT_URL = "http://www.google.com/m8/feeds/contacts/";
	private Logger logger = Logger.getLogger(GmailConnector.class);
	
	public GmailConnector(User user)
	{
		this.user = user;
	}

	@Override
	protected void login() throws ConnectorException
	{
	}

	@Override
	protected void logout() throws ConnectorException
	{
	}

	@Override
	public List<Contact> getContacts() throws ConnectorException
	{
		ContactsService service = new ContactsService("MailConnector");
		try
		{
			service.setUserCredentials(user.getUsername(), user.getPassword());
		} catch (AuthenticationException e)
		{
			throw new ConnectorException(ConnectorInterface.ERR_MSG_LOGIN, e);
		}
		
		logger.info("Login as " + user.getUsername() + " succeed.");
		
		try
		{
			URL url = new URL(CONNECT_URL + user.getUsername() + "/full");
			Query query = new Query(url);
			query.setMaxResults(Integer.MAX_VALUE);
			ContactFeed feed = service.query(query, ContactFeed.class);

			List<Contact> contacts = new ArrayList<Contact>();
			for (ContactEntry entry : feed.getEntries())
			{
				for (Email email : entry.getEmailAddresses())
				{
					String address = email.getAddress();
					String name = null;
					if (entry.hasName())
					{
						name = entry.getName().getFullName().getValue();
					} else
					{
						name = getUsername(address);
					}
					contacts.add(new Contact(name, address));
				}
			}
			
			logger.info("Get " + user.getUsername() + "'s contacts succeed.");
			return contacts;
		} catch (Exception e)
		{
			throw new ConnectorException(ConnectorInterface.ERR_MSG_UNKNOWN, e);
		}
	}
}
